package com.hanbang.taitaigou.ui.activity.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;
import com.hanbang.taitaigou.utils.ui.ToastUtils;


public class ForgetPasswordActivity extends BaseActivity {

    // 验证码按钮倒计时 总时长和间隔时长
    private long totalTime = 120000, intervalTime = 1000;
    private TextView authCodeButton;
    private EditText phoneEt;
    private EditText codeEt;
    private EditText passwordEt;
    private EditText password2Et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forget_password);
        initView();
        setOnClickListener();
    }

    /*
     * 设置点击事件
     */
    private void setOnClickListener() {
        authCodeButton.setOnClickListener(onClickListener);
        findViewById(R.id.ok).setOnClickListener(onClickListener);

    }

    private OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.user_register_auth_code_button:
                    sendYZM(v);
                    break;
                case R.id.ok:
                    ok();
                    break;

                default:
                    break;
            }
        }
    };

    /*
     * 初始化view
     */
    private void initView() {
        // 设置顶部的标题
        setBackArrows(findViewById(R.id.top_bar_left));
        setTop(null, R.mipmap.back, "忘记密码", null, 0);
        authCodeButton = (TextView) findViewById(R.id.user_register_auth_code_button);
        phoneEt = (EditText) findViewById(R.id.phone);
        codeEt = (EditText) findViewById(R.id.code);
        passwordEt = (EditText) findViewById(R.id.password);
        password2Et = (EditText) findViewById(R.id.password2);

    }

    CountDownTimer countDownTimer = new CountDownTimer(totalTime, intervalTime) {
        @Override
        public void onFinish() {// 计时完毕时触发
            authCodeButton.setText("重新发送");
            authCodeButton.setEnabled(true);
            // authCodeButton.setClickable(true);
        }

        @Override
        public void onTick(long currentTime) {// 计时过程显示
            // authCodeButton.setClickable(false);
            authCodeButton.setEnabled(false);
            authCodeButton.setText(currentTime / 1000 + "秒");
        }
    };

    private void sendYZM(View v) throws IndexOutOfBoundsException {
        final String res = phoneEt.getText().toString().trim();
        if (res == null || res.equals("") || res.length() < 11) {
            ToastUtils.show(ForgetPasswordActivity.this, "请正确输入手机号！",
                    Toast.LENGTH_LONG);
            return;
        }
    }

    /*
     * http://localhost:2642/tools/UserLogin.ashx?action=user_update_pwd&mobile=
     * 18913114602&code=8687&password=123456&againpassword=123456
     */
    private void ok() {
        final String res = phoneEt.getText().toString().trim();
        final String res2 = codeEt.getText().toString().trim();
        final String pass1 = passwordEt.getText().toString().trim();
        final String pass2 = password2Et.getText().toString().trim();
        if (res == null || res.equals("") || res.length() < 11) {
            ToastUtils.show(ForgetPasswordActivity.this, "请正确输入手机号！",
                    Toast.LENGTH_LONG);
            return;
        }
        if (res2 == null || res2.equals("") || res2.length() < 1) {
            ToastUtils.show(ForgetPasswordActivity.this, "请输入验证码",
                    Toast.LENGTH_LONG);
            return;
        }
        if (pass1 == null || pass1.equals("") || pass1.length() < 6) {
            ToastUtils.show(ForgetPasswordActivity.this, "请输入密码",
                    Toast.LENGTH_LONG);
            return;
        }
        if (pass2 == null || pass2.equals("") || pass2.length() < 6) {
            ToastUtils.show(ForgetPasswordActivity.this, "请再次输入密码",
                    Toast.LENGTH_LONG);
            return;
        }
        if (!pass1.trim().equals(pass2.trim())) {
            ToastUtils.show(ForgetPasswordActivity.this, "两次密码不一致",
                    Toast.LENGTH_LONG);
            return;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
